package com.light.netty.server.handler;

import com.light.netty.server.security.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.nio.charset.StandardCharsets;

// 自定义业务处理器（线程池隔离）
public class TcpBusinessHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        System.out.println("收到客户端消息: " + msg);

        String responseMsg = "receive ack:" + msg;
        ctx.writeAndFlush(responseMsg + "\r\n").addListener(future -> {
            if (future.isSuccess()) {
                System.out.println("TCP Server Message sent successfully");
            } else {
                System.err.println("TCP Server Failed to send message: " + future.cause());
            }
        });
    }

    private void processMessage(MessageProtocol msg) throws InterruptedException {
        Thread.sleep(1000); // 模拟耗时操作
        System.out.println("服务端业务处理..........");
    }
}